import Vue from "vue";
import Vuex from "vuex";
import statics from "./statics.js";
import axios from "axios";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    screenSize: { width: 0, height: 0 },
    minimumDesktopWidth: 540,
    loggedIn: false,
    loginData: {
      input: null,
      respones: null,
    },
    loginLevel: 0,
    params: {},
    exercises: [],
    exercisesValid: [],
    exercisesQueried: [],
    records: [],
    traineeRecords: [],
    coachRecords: [],
    userList: [],
    queriedRecords: [],
    existedRecordTitle: [],
    statisticTrainee: {},
    statisticExercise: {},
  },
  getters: {
    isDesktop: (state) => state.screenSize.width > state.minimumDesktopWidth,
    dialogWidth: (state) =>
      state.screenSize.width > 620
        ? 600 + "px"
        : state.screenSize.width - 20 + "px",
    userName: (state) => state.loginData.respones.userbase.name,
    userNumber: (state) => state.loginData.respones.userbase.number,
    userLevel: (state) => state.loginData.respones.userbase.level,
    userInfo: (state) => state.loginData.respones.userinfo,
    user: (state) => ({
      string: state.loginData.respones.userbase.number,
      number: state.loginData.respones.userbase.number,
      name: state.loginData.respones.userbase.name,
      passwd: state.loginData.respones.userbase.passwd,
    }),
  },
  mutations: {
    setScreenSize(state, size) {
      state.screenSize = size;
    },
    setLoginData(state, payload) {
      state.loginData = payload;
    },
    setLoggedIn(state, payload) {
      state.loggedIn = payload;
    },
    setLoginLevel(state, payload) {
      state.loginLevel = payload;
    },
    setParams(state, payload) {
      state.params = payload;
    },
    setParam(state, payload) {
      state.params[payload.name] = payload.content;
    },
    setExercises(state, payload) {
      state.exercises = payload;
    },
    setExercisesValid(state, payload) {
      state.exercisesValid = payload;
    },
    setExercisesQueried(state, payload) {
      state.exercisesQueried = payload;
    },
    setRecords(state, payload) {
      state.records = payload;
    },
    setTraineeRecords(state, payload) {
      state.traineeRecords = payload;
    },
    setCoachRecords(state, payload) {
      state.coachRecords = payload;
    },
    setUserList(state, payload) {
      state.userList = payload;
    },
    setQueriedRecords(state, payload) {
      state.queriedRecords = payload;
    },
    setCoach(state, payload) {
      state.userList[payload.index].TREX_LEVEL = payload.TREX_LEVEL;
    },
    setExistedRecordTitle(state, payload) {
      state.existedRecordTitle = payload;
    },
    setStatisticTrainee(state, payload) {
      state.statisticTrainee = payload;
    },
    setStatisticExercise(state, payload) {
      state.statisticExercise = payload;
    },
  },
  actions: {
    login(context, payload) {
      console.log("login action", payload);
      return new Promise((resolve) => {
        axios
          .post("api/login.php", { user: payload })
          .then((respones) => {
            // console.log(respones.status === 200 ? respones.data : respones);
            if (respones.data.status == 0) {
              context.commit("setLoginData", {
                input: payload,
                respones: respones.data,
              });
              context.commit("setLoggedIn", true);
              context.commit(
                "setLoginLevel",
                respones.data.userinfo.TREX_LEVEL
              );
              context.dispatch("getParams");
              context.dispatch("getUserList");
              context.dispatch("getExercises");
            }
            resolve(respones.data.status);
          })
          .catch((error) => {
            console.log(error);
          });
      });
    },
    getParams(context) {
      return new Promise((resolve, reject) => {
        axios
          .get("api/get_params.php")
          .then((respones) => {
            context.commit("setParams", respones.data);
            resolve(respones.data);
            // console.log(respones.data);
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
    saveParam(context, payload) {
      let user = context.state.loginData.input;
      return new Promise((resolve) => {
        axios.post("api/save_param.php", { user, payload }).then((respones) => {
          // console.log(respones.data);
          context.commit("setParam", payload);
          resolve(respones.data);
        });
      });
    },
    getExercises(context) {
      return new Promise((resolve) => {
        axios.get("api/get_exercises.php").then((respones) => {
          context.commit("setExercises", respones.data);
          // console.log(respones.data);
          resolve(respones.data);
        });
      });
    },
    getExercisesValid(context) {
      return new Promise((resolve) => {
        axios.get("api/get_exercises_valid.php").then((respones) => {
          context.commit("setExercisesValid", respones.data);
          // console.log(respones.data);
          resolve(respones.data);
        });
      });
    },
    queryExercises(context, payload) {
      let user = context.getters.user;
      return new Promise((resolve, reject) => {
        axios
          .post("api/query_exercises.php", { user, payload })
          .then((respones) => {
            context.commit("setExercisesQueried", respones.data);
            // console.log(respones.data);
            resolve(respones.data);
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
    saveExercise(context, payload) {
      let user = context.state.loginData.input;
      return new Promise((resolve, reject) => {
        axios
          .post("api/save_exercise.php", { user, payload })
          .then((respones) => {
            // context.dispatch("getExercises").then(exercises => {
            //   resolve(exercises)
            // });
            resolve(respones.data);
            console.log(respones.data);
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
    getTraineeRecords(context, payload) {
      return new Promise((resolve) => {
        context.dispatch("getRecords", payload).then((records) => {
          context.commit("setTraineeRecords", records);
          resolve(records);
        });
      });
    },
    getCoachRecords(context, payload) {
      return new Promise((resolve) => {
        context.dispatch("getRecords", payload).then((records) => {
          context.commit("setCoachRecords", records);
          resolve(records);
        });
      });
    },
    getRecords(context, payload) {
      console.log("getRecords", payload);
      let user = context.state.loginData.input;
      return new Promise((resolve) => {
        axios
          .post("api/query_records.php", { user, payload })
          .then((respones) => {
            console.log("getRecords", respones.data);
            if (respones.data && respones.data.length > 0) {
              context.commit("setRecords", respones.data);
            }
            resolve(respones.data);
          })
          .catch((error) => {
            console.log(error);
          });
      });
    },
    saveRecord(context, payload) {
      let user = context.getters.user;
      return new Promise((resolve, reject) => {
        axios
          .post("api/save_record.php", { user, payload })
          .then((respones) => {
            console.log(respones.data);
            resolve();
            // context.dispatch("getRecords", payload.exercise).then(records => {
            //   resolve(records);
            // });
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
    getUserList(context) {
      return new Promise((resolve) => {
        axios.get("api/get_userlist.php").then((respones) => {
          // console.log(respones.data);
          context.commit("setUserList", respones.data);
          resolve(respones.data);
        });
      });
    },
    queryRecords(context, payload) {
      let user = context.getters.user;
      return new Promise((resolve, reject) => {
        axios
          .post("api/query_records_lite.php", { user, payload })
          .then((respones) => {
            console.log(respones.data);
            let data = respones.data;
            if (data == "") data = [];
            context.commit("setQueriedRecords", data);
            resolve();
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
    setCoach(context, payload) {
      let user = context.getters.user;
      // console.log('user', user);
      // console.log('setCoach', payload);
      return new Promise((resolve, reject) => {
        axios
          .post("api/set_coach.php", { user, payload })
          .then((respones) => {
            console.log(respones.data);
            context.commit("setCoach", payload);
            resolve();
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
    importRecord(context, payload) {
      let user = context.getters.user;
      return new Promise((resolve, reject) => {
        axios
          .post("api/import_record.php", { user, payload })
          .then((respones) => {
            resolve(respones.data);
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
    importExercise(context, payload) {
      let user = context.getters.user;
      return new Promise((resolve, reject) => {
        axios
          .post("api/import_exercise.php", { user, payload })
          .then((respones) => {
            resolve(respones.data);
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
    getExistedRecordTitle(context) {
      let user = context.getters.user;
      return new Promise((resolve, reject) => {
        axios
          .post("api/get_existed_record_title.php", { user })
          .then((respones) => {
            context.commit("setExistedRecordTitle", respones.data);
            resolve(respones.data);
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
    getStatisticTrainee(context, payload) {
      let user = context.getters.user;
      return new Promise((resolve, reject) => {
        axios
          .post("api/get_statistic_trainee.php", { user, payload })
          .then((respones) => {
            context.commit("setStatisticTrainee", respones.data);
            resolve(respones.data);
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
    getStatisticExercise(context, payload) {
      let user = context.getters.user;
      return new Promise((resolve, reject) => {
        axios
          .post("api/get_statistic_exercise.php", { user, payload })
          .then((respones) => {
            context.commit("setStatisticExercise", respones.data);
            resolve(respones.data);
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
    saveUserInfo(context, payload) {
      let user = context.getters.user;
      return new Promise((resolve, reject) => {
        axios
          .post("api/save_user_info.php", { user, payload })
          .then((respones) => {
            console.log("saveUserInfo", respones.data);
            context
              .dispatch("login", { string: user.number, passwd: user.passwd })
              .then((res) => {
                resolve(res);
              });
          })
          .catch((error) => {
            console.log(error);
            reject(error);
          });
      });
    },
  },
  modules: {
    namespaced: true,
    statics,
  },
});
